#coding: utf-8
class Admin::TerminalsController < AdminController

  before_filter :check_access
  before_filter :setup
  set_tab :terminals
  respond_to :html, :json

  def index
    @terminal_group = TerminalGroup.where(id: params[:terminal_group_id]).first if params[:terminal_group_id].present?
    @city = @terminal_group.city if @terminal_group
    @terminals = Terminal
    @terminals = @terminals.where(terminal_group_id: params[:terminal_group_id])
    @terminals = @terminals.order('created_at desc').page(params[:page])
    eval %{set_tab :city_#{@city.id}, :sub_nav}
    @ways = ["设备",@city.province.name,@city.name,@terminal_group.group_name,"终端管理"]
    render :layout => "terminal_groups"
  end

  def show
  	@terminal = Terminal.find(params[:id])
  end

  def remove
    @terminal = Terminal.find(params[:id])
    @terminal.terminal_group_id = nil
    @terminal.save!
  end

  private

  def setup
    @left_panel = "admin/areas/area_widget"
  end

  def check_access
    if params[:terminal_group_id].present?
      @terminal_group = TerminalGroup.find_by_id(params[:terminal_group_id])
      unless current_admin.can_access?(@terminal_group)
        render :file => "public/403.html", :status => 403
      end
    end
  end

end
